#coding=utf-8
import requests,json,urllib
import re
import random
import time
#官网后台管理系统登录接口并返回带token的headers
def login(username):
    headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
               ,'Accept-Encoding': 'gzip, deflate','Cache-Control': 'max-age=0'
                ,'Accept-Language': 'zh-CN,zh;q=0.9','User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
               ,'Upgrade-Insecure-Requests': '1',
               'Connection': 'keep - alive','Host': 'ow.api.test.gdgyun.com'}
    s = requests.session()
    login_data = ({"username":username,"password":"2c29077fa493d837b388f5e6792d09fa","verifyCode":"1234"})
    login = s.post('http://ow.api.test.gdgyun.com/login/submit',json=login_data)
    #index = s.get('http://ow.api.test.gdgyun.com/manage/article/add.html?action=detail&category=0&id=15',cookies=login.cookies,headers=headers)
    return login.cookies   #返回cookies

#上传文章
def put_article(username,category,orders):   #登录用户名，分类：0为媒体报道、1为公司动态、2为行业信息，显示顺序
    headers = {'Accept': 'application/json, text/javascript, */*; q=0.01'
                , 'Accept-Encoding': 'gzip, deflate','X-Requested-With': 'XMLHttpRequest'
                , 'Accept-Language': 'zh-CN,zh;q=0.9','Origin': 'http://ow.api.test.gdgyun.com',
               'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
                , 'Upgrade-Insecure-Requests': '1',
               'Connection': 'keep - alive', 'Host': 'ow.api.test.gdgyun.com'}
    cookies = login(username)
    s = requests.session()
    try:
        #从腾讯新闻网站随机爬取一篇文章
        index = s.get('https://news.qq.com/')
        url = re.findall(r'a target="_blank" class="linkto" href="(.+)"', index.text)  # 获得新闻的url
        num = len(url)
        for i in range(0, int(num), 1):
            id = random.randint(0, int(num))
            index_news = s.get(url[id])
            title = re.findall(r'<title>(.+)</title>', index_news.text)[0]  # 获得新闻的标题
            content = re.findall(r'<p class="one-p">(.+)</p>', index_news.text)  # 获得文章的内容
            article = ''.join(content)
            if article == '':
                print u'重新获取文章'
                continue
            else:
                print article
                break
        data = ({"id":"","title":str(title),"digest":str(content[0]),"category":str(category),"orders":orders
            ,"status":"0","content":article
            ,"resourcesInfo":"http://ow.api.test.gdgyun.com:80/uploads/temp/INDUSTRY_INFORMATION/79ebad5b488152fbadfdf8124a2a11a27fa62df5.jpg"})
        if category == '0':  #为媒体报道
            put = s.post('http://ow.api.test.gdgyun.com/manage/media/save',json=data,cookies=cookies,headers=headers)
        if category == '1':  #为公司动态
            put = s.post('http://ow.api.test.gdgyun.com/manage/dynamic/save', json=data, cookies=cookies, headers=headers)
        if category == '2':  #为行业信息
            put = s.post('http://ow.api.test.gdgyun.com/manage/industry/save', json=data, cookies=cookies, headers=headers)
        return put.text+u'，文章标题是：'+str(title)+u'，显示顺序为第'+orders+u'位'
    except:
        return u'获取的文章标题有误，请重新运行一次'

#发布职位信息
def put_position(username,name,department,orders,status):   #后台登录名、职位名称、所属部门：0运营部，1运维部，2测试部，3开发部，4产品部，5设计部、显示顺序、状态：1为草稿，0为发布
    now = time.strftime("%Y-%m-%d", time.localtime(time.time()))
    headers = {'Accept': 'application/json, text/javascript, */*; q=0.01'
        , 'Accept-Encoding': 'gzip, deflate', 'X-Requested-With': 'XMLHttpRequest'
        , 'Accept-Language': 'zh-CN,zh;q=0.9', 'Origin': 'http://ow.api.test.gdgyun.com',
               'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
        , 'Upgrade-Insecure-Requests': '1',
               'Connection': 'keep - alive', 'Host': 'ow.api.test.gdgyun.com'}
    cookies = login(username)
    s = requests.session()
    data = ({"id":"","name":name,"department":department,"address":"成都市高新区香年广场T2-46楼"
        ,"salaryPackage":"15-28K","recruitingNumbers":"3","educationBackground":"本科及以上"
        ,"publishTime":now+"T00:00:00.000Z","validityTime":"2018-12-31T00:00:00.000Z"
        ,"orders":orders,"ageRestrict":"20及以上","responsibility":name+"的岗位职责岗位职责岗位职责","qualification":name+"的任职资格任职资格任职资格","status":status})
    put = s.post('http://ow.api.test.gdgyun.com/manage/position/save',json=data,cookies=cookies,headers=headers)
    if status == '0':
        status = u'发布'
    if status == '1':
        status = u'草稿'
    return put.text+u'+发布的职位是：'+str(name)+u'，显示顺序为第'+str(orders)+u'位'+u'，状态为：'+str(status)

if __name__ =='__main__':
    for i in range(10,99,1):
        put_article('chenqiang','0',str(i))
    #put_position('chenqiang','测试工程师','2','3','0')